import 'dart:convert';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBNavigator.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:fluwx/fluwx.dart' as fluwx;
import 'dart:io';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBBaseModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBLogUtil.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBBaseMineNetWork/YBBaseMineNetWork.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBLoginInfoModule/YBLoginInfoApi.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineAuthVipModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineUserInfoModel.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';


class YBLoginOtherPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    Widget iOSWidgetAppleW;

    if (Platform.isIOS){
       iOSWidgetAppleW = Container(
         child: SignInWithAppleButton(
             height: YBTextStyle.adapterSize44,
             style: SignInWithAppleButtonStyle.whiteOutlined,
             borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize22)),
             text: "apple登录",
             onPressed: () async{
              final credential = await SignInWithApple.getAppleIDCredential(
                  scopes:[
                    AppleIDAuthorizationScopes.fullName,
                    AppleIDAuthorizationScopes.email
                  ],
                  webAuthenticationOptions: WebAuthenticationOptions(
                    clientId: "com.sail.app.benlaitongji",
                    redirectUri:Uri.parse(
                      "https://flutter-sign-in-with-apple-example.glitch.me/callbacks/sign_in_with_apple"
                    ),
                  ),
                 nonce: "zsyj-nonce",
                 state: "zsyj-state",
              );
              _applePhone(utf8.decode(credential.identityToken.codeUnits), context, credential.userIdentifier, "${credential.familyName}${credential.givenName}");
             }
         ),
       );
    }else{
      iOSWidgetAppleW = Container();
    }




    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            height: YBTextStyle.adapterSize30,
          ),
          Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  height: 1,
                  width: YBTextStyle.adapterSize60,
                  color: YBColor.colorD8D8D8,
                ),
                Container(
                  width: YBTextStyle.adapterSize10,
                ),
                Container(
                  child: Text("其他方式登录",style:YBTextStyle.loginOtherText8F939A12,),
                ),
                Container(
                  width: YBTextStyle.adapterSize10,
                ),
                Container(
                  height: 1,
                  width: YBTextStyle.adapterSize60,
                  color: YBColor.colorD8D8D8,
                ),
              ],
            ),
          ),
          Container(
            height: YBTextStyle.adapterSize15,
          ),
          Container(
            height: YBTextStyle.adapterSize44,
            child: OutlineButton(
                shape: RoundedRectangleBorder(
                    side: BorderSide(
                        color:YBColor.color181818,
                        width: YBTextStyle.adapterSize1
                    ),
                    borderRadius: BorderRadius.circular(YBTextStyle.adapterSize22)
                ),
                borderSide: BorderSide(
                    color:YBColor.color181818,
                    style:BorderStyle.solid
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      child: Image.asset(YBImageStyle.getImagePath("commonShare_wechatImage"),width: YBTextStyle.adapterSize20,height: YBTextStyle.adapterSize20,),
                    ),
                    Container(
                      width: 5,
                    ),
                    Container(
                      child: Text("通过微信登录",style: YBTextStyle.loginWeChatText00000014,),
                    ),
                  ],
                ),
                onPressed:(){
                  fluwx.sendWeChatAuth(scope: "snsapi_userinfo",state: "wechat_sdk_demo_test").then((value){
                    print("----object---$value---");

                    fluwx.weChatResponseEventHandler.listen((event) {
                      print("----------");
                      if(event is fluwx.WeChatAuthResponse){
                        print('object--新的端口获取值----${event.code}----');
                        if (event.code != null) {
                          getWeChat(event.code,context);
                        }
                      }
                    });

                  });
                }
            ),
          ),
          Container(
            height: YBTextStyle.adapterSize16,
          ),
          Container(
            height: YBTextStyle.adapterSize44,
            child: OutlineButton(
                shape: RoundedRectangleBorder(
                  side: BorderSide(
                    color:YBColor.color181818,
                    width: YBTextStyle.adapterSize1
                  ),
                  borderRadius: BorderRadius.circular(YBTextStyle.adapterSize22)
                ),
                borderSide: BorderSide(
                  color:YBColor.color181818,
                  style:BorderStyle.solid
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [

                    Container(
                      child: Image.asset(YBImageStyle.getImagePath("login_password"),width: YBTextStyle.adapterSize20,height: YBTextStyle.adapterSize20,),
                    ),
                    Container(
                      width: YBTextStyle.adapterSize5,
                    ),
                    Container(
                      child: Text("使用密码登录",style: YBTextStyle.loginWeChatText00000014),
                    ),
                  ],
                ),
                onPressed: (){
                  YBNavigatorUtil.jump(context, "/loginIn/loginPasswordPage");
                }
            ),
          ),
          Container(
            height: YBTextStyle.adapterSize16,
          ),
          iOSWidgetAppleW,
        ],
      ),
    );
  }

  void getWeChat(String code,BuildContext context){
    YBLoginInfoApi.loginInWxChat({
      "sysCode":"xg_youbang",
      'code':code
    },success:(data){
      print("微信登录------$data");
      YBBaseModel dataCode = YBBaseModel.fromJson(data);
      if (dataCode.code == 0){
        SpUtil.putString("token",dataCode.body);
        _isPhoneNumberDisplay(context);
      }
    },fail: (error){
      print("微信登录------$error");
    });
  }

  void _isPhoneNumberDisplay(BuildContext context){
    YBMineNetWork.mineUserInfoData({
      "sysCode":"xg_newyijiwang",
    },success:(data){
      YBLogUtils.e('--个人信息---$data--');
      var mineUserInfoModel = YBMineUserInfoModel.fromJson(data);
      if (mineUserInfoModel.customerPhone != null) {
        // YBApplication.router.navigateTo(context, '/',replace: true);
        if (mineUserInfoModel.customerAuthorities != null) {
          for (YBMineAuthVipModel mineAuthModel in mineUserInfoModel
              .customerAuthorities) {
            if (mineAuthModel.authority.contains('BLVIP')) {
              SpUtil.putString("VIP", "1");
              break;
            }
          }
        }
        EasyLoading.showSuccess('登录成功',duration: Duration(seconds: 1));
        YBNavigatorUtil.goToHomeRemovePage(context);
      }else{
        YBApplication.router.navigateTo(context, '/loginIn/loginBangDingPage');
      }
    },fail: (error){
      print('--$error-');
    });
  }

  void _applePhone(String appldId,BuildContext context,String authCode,String nickName){
    YBLoginInfoApi.loginInApplePhoneData({
      "sysCode":"xg_youbang",
      "accessToken":appldId,
      "appleUserId":authCode,
      "nickName":nickName
    },success: (data){
      String codeStr = data;
      if (!ObjectUtil.isEmptyString(codeStr)){
        SpUtil.putString("token", codeStr);
        _isPhoneNumberAppleDisplay(context);
      }
    },fail: (error){

    });
  }


  void _isPhoneNumberAppleDisplay(BuildContext context){
    YBMineNetWork.mineUserInfoData({
      "sysCode":"xg_newyijiwang",
    },success:(data){
      YBLogUtils.e('--个人信息---$data--');
      var mineUserInfoModel = YBMineUserInfoModel.fromJson(data);
      if (mineUserInfoModel.customerAuthorities != null) {
        for (YBMineAuthVipModel mineAuthModel in mineUserInfoModel
            .customerAuthorities) {
          if (mineAuthModel.authority.contains('BLVIP')) {
            SpUtil.putString("VIP", "1");
            break;
          }
        }
      }
      EasyLoading.showSuccess('登录成功',duration: Duration(seconds: 1));
      YBNavigatorUtil.goToHomeRemovePage(context);
    },fail: (error){
      print('--$error-');
    });
  }

}


